package com.young.ums.web;

import com.young.common.util.CollectionUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.annotation.Log;
import com.young.ums.model.Dept;
import com.young.ums.model.enums.EDeptTypeEnum;
import com.young.ums.service.*;
import com.young.ums.util.AuthHelper;
import com.young.ums.util.UmsConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 获取树控制器
 * @author imrookie
 * @date 2018/10/9
 */
@Controller
@RequestMapping(value = "/ums/tree", method = {RequestMethod.GET, RequestMethod.POST})
public class TreeController {

    private static final Logger logger = LoggerFactory.getLogger(TreeController.class);

    @Autowired
    IUmsTreeService umsTreeService;

    /**
     * 获取单位角色树
     * @param dept
     * @param request
     * @return
     */
    @Log("获取单位角色树")
    @RequestMapping("/getRoleTree")
    @ResponseBody
    public Object getRoleTree(Dept dept, HttpServletRequest request) {
        //权限启用参数没有传时默认启用权限设置
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);
        //单位查询
        dept.setType(EDeptTypeEnum.COMPANY.type);
        return Result.buildSuccess(umsTreeService.getRoleTree(dept));
    }

    /**
     * 获取可以使用的单位角色树(角色用户页面)
     * @param dept
     * @param request
     * @return
     */
    @Log("获取可以使用的单位角色树(含公共角色)")
    @RequestMapping("/getRoleTreeContainsCommonRole")
    @ResponseBody
    public Object getRoleTreeContainsCommonRole(Dept dept, HttpServletRequest request) {
        //权限启用参数没有传时默认启用权限设置
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);
        //单位查询
        dept.setType(EDeptTypeEnum.COMPANY.type);

        // 角色树
        List<Dept> list = umsTreeService.getRoleTree(dept);
        if (list == null || list.size() == 0){
            return null;
        }

        //查询可以使用的公共角色
        if (StringUtils.isNotBlank(dept.getLoginUserId())) {
            List<Dept> commonList = umsTreeService.getCommonRoleTree(dept);
            if (CollectionUtil.isNotEmpty(commonList)) {
                list.addAll(commonList);
            }
        }

        return Result.buildSuccess(list);
    }

    /**
     * 获取单位用户树
     * @param dept
     * @param request
     * @return
     */
    @Log("获取单位用户树")
    @RequestMapping("/getCompanyUserTree")
    @ResponseBody
    public Object getCompanyUserTree(Dept dept, HttpServletRequest request) {
        //权限启用参数没有传时默认启用权限设置
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);
        //单位查询
        dept.setType(EDeptTypeEnum.COMPANY.type);
        return Result.buildSuccess(umsTreeService.getCompanyUserTree(dept));
    }

    /**
     * 获取单位树
     * @param dept
     * @param rootId 树的根节点
     * @param request
     * @return
     */
    @Log("获取单位树")
    @RequestMapping("/getCompanyTree")
    @ResponseBody
    public Object getCompanyTree(Dept dept, String rootId, HttpServletRequest request) {
        return Result.buildSuccess(umsTreeService.getCompanyTree(dept, rootId));
    }

    /**
     * 获取部门树
     * @param dept
     * @param request
     * @return
     */
    @Log("获取部门树")
    @RequestMapping("/getDeptTree")
    @ResponseBody
    public Object getDeptTree(Dept dept, HttpServletRequest request) {
        //权限启用参数没有传时默认启用权限设置
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);
        //部门查询
        dept.setType(EDeptTypeEnum.DEPT.type);
        return Result.buildSuccess(umsTreeService.getDeptTree(dept));
    }

    /**
     * 获取组织机构树
     * @param dept
     * @param request
     * @return
     */
    @Log("获取组织机构树")
    @RequestMapping("/getOrgTree")
    @ResponseBody
    public Object getOrgTree(Dept dept, HttpServletRequest request) {
        //权限启用参数没有传时默认启用权限设置
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);
        return Result.buildSuccess(umsTreeService.getDeptTree(dept));
    }
}
